package com.ruoyi.assets.controller;

import com.ruoyi.assets.domain.Assetsinformation;
import com.ruoyi.assets.service.IAssetsinformationService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资产信息Controller
 *
 * @author ruoyi
 * @date 2023-05-04
 */
@RestController
@RequestMapping("/assets/assetsinformation")
public class AssetsinformationController extends BaseController {
    @Autowired
    private IAssetsinformationService assetsinformationService;

    /**
     * 查询资产信息列表
     */
    @PreAuthorize("@ss.hasPermi('assets:assetsinformation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Assetsinformation assetsinformation) {
        startPage();
        List<Assetsinformation> list = assetsinformationService.selectAssetsinformationList(assetsinformation);
        return getDataTable(list);
    }

    /**
     * 导出资产信息列表
     */
    @PreAuthorize("@ss.hasPermi('assets:assetsinformation:export')")
    @Log(title = "资产信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Assetsinformation assetsinformation) {
        List<Assetsinformation> list = assetsinformationService.selectAssetsinformationList(assetsinformation);
        ExcelUtil<Assetsinformation> util = new ExcelUtil<Assetsinformation>(Assetsinformation.class);
        util.exportExcel(response, list, "资产信息数据");
    }

    /**
     * 获取资产信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('assets:assetsinformation:query')")
    @GetMapping(value = "/{AssetsInformationID}")
    public AjaxResult getInfo(@PathVariable("AssetsInformationID") Long AssetsInformationID) {


        return success(assetsinformationService.selectAssetsinformationByAssetsInformationID(AssetsInformationID));
    }


    /**
     * 新增资产信息
     */
    @PreAuthorize("@ss.hasPermi('assets:assetsinformation:add')")
    @Log(title = "资产信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Assetsinformation assetsinformation) {

//        System.out.println(assetsinformation);

        assetsinformation.setCreateUserName(getUsername());
        assetsinformation.setCreateUserID(getUserId());
        String devNo = assetsinformationService.selectAssetsinformationLastindex().getDevNo();
        String[] strs = devNo.split("[^0-9]");//根据不是数字的字符拆分字符串
        String numStr = strs[strs.length - 1];//取出最后一组数字
        int n = numStr.length();//取出字符串的长度
        int num = Integer.parseInt(numStr) + 1;//将该数字加一
        String added = String.valueOf(num);
        n = Math.min(n, added.length());
        //拼接字符串
        String a = devNo.subSequence(0, devNo.length() - n) + added;

        assetsinformation.setDevNo(a);

        System.out.println(assetsinformation);
        return toAjax(assetsinformationService.insertAssetsinformation(assetsinformation));
//        return success(assetsinformation);
    }

    /**
     * 修改资产信息
     */
    @PreAuthorize("@ss.hasPermi('assets:assetsinformation:edit')")
    @Log(title = "资产信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Assetsinformation assetsinformation) {
        assetsinformation.setEditName(getUsername());
        assetsinformation.setEditUserID(getUserId());
        return toAjax(assetsinformationService.updateAssetsinformation(assetsinformation));
    }

    /**
     * 删除资产信息
     */
    @PreAuthorize("@ss.hasPermi('assets:assetsinformation:remove')")
    @Log(title = "资产信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{AssetsInformationIDs}")
    public AjaxResult remove(@PathVariable Long[] AssetsInformationIDs) {
        return toAjax(assetsinformationService.deleteAssetsinformationByAssetsInformationIDs(AssetsInformationIDs));
    }



}
